package com.huika.renrench.control.my.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.android.volley.Response.Listener;
import com.google.gson.reflect.TypeToken;
import com.huika.lib.utils.MoneyShowTool;
import com.huika.renrench.GlobalApp;
import com.huika.renrench.R;
import com.huika.renrench.config.UrlConstants;
import com.huika.renrench.control.common.base.BaseAct;
import com.huika.renrench.http.FormResultRequest;
import com.huika.renrench.http.JSONWrapAjaxParams;
import com.huika.renrench.http.RequestResult;
import com.huika.renrench.support.bean.TransactionRecordBean;
import com.huika.renrench.utils.DateTimeTool;
import com.huika.renrench.view.TitleBarHelper;

/**
 * 
 * @description：交易记录详情
 * @author zc
 * @date 2015年6月29日 下午5:36:37
 */
public class TransactionDetailActivity extends BaseAct implements Listener<RequestResult<TransactionRecordBean>> {

	private TransactionRecordBean recordBean;
	private String userId;
	private ImageView icon;
	private TextView trade_flag;
	private TextView trade_reason;
	private TextView name_detail;
	private TextView trade_amount_detail;
	private View layout1, layout2, layout3, layout4;
	private TextView bank_name_detail;
	private TextView bank_no_detail;
	private TextView recharge_channel_detail;
	private TextView opposite_info_detail;
	private TextView serial_number_detail;
	private TextView order_no_detail;
	private TextView trade_time_detail;
	private TextView withdraw_poundage_detail;
	private View poundage_layout;
	private boolean isRemit = false;
	
	@Override
	public void setRootView() {
		setContentView(R.layout.activity_transaction_detail);
	}
	
	@Override
	protected void initData() {
		super.initData();
		TitleBarHelper barHelper = new TitleBarHelper(this, -1);
		barHelper.setLeftMsg("返回");
		barHelper.setTitleMsg("交易详情");
		userId = GlobalApp.userModel.getUserId();
		recordBean = (TransactionRecordBean) getIntent().getSerializableExtra("recordBean");
	}
	
	@Override
	protected void initWidget() {
		super.initWidget();
		icon = (ImageView) findViewById(R.id.icon);
		trade_flag = (TextView) findViewById(R.id.trade_flag);
		trade_reason = (TextView) findViewById(R.id.trade_reason);
		name_detail = (TextView) findViewById(R.id.name_detail);
		trade_amount_detail = (TextView) findViewById(R.id.trade_amount_detail);
		bank_name_detail = (TextView) findViewById(R.id.bank_name_detail);
		bank_no_detail = (TextView) findViewById(R.id.bank_no_detail);
		recharge_channel_detail = (TextView) findViewById(R.id.recharge_channel_detail);
		opposite_info_detail = (TextView) findViewById(R.id.opposite_info_detail);
		serial_number_detail = (TextView) findViewById(R.id.serial_number_detail);
		order_no_detail = (TextView) findViewById(R.id.order_no_detail);
		trade_time_detail = (TextView) findViewById(R.id.trade_time_detail);
		withdraw_poundage_detail = (TextView) findViewById(R.id.withdraw_poundage_detail);
		poundage_layout = findViewById(R.id.poundage_layout);
		layout1 =  findViewById(R.id.layout1);
		layout2 =  findViewById(R.id.layout2);
		layout3 =  findViewById(R.id.layout3);
		layout4 =  findViewById(R.id.layout4);
		
		
		if(recordBean != null) {
			getTransactionDetail(userId, recordBean.getCapitalType(), recordBean.capitalId);
		}
	}
	
	private String getMsg(int capitalType) {
		String msg = "";
		switch (capitalType) {
			case 1:
				msg = "充值";
				layout3.setVisibility(View.VISIBLE);
				break;
			case 2:
			case 6:
				msg = "提现";
				layout1.setVisibility(View.VISIBLE);
				layout2.setVisibility(View.VISIBLE);
				break;
			case 3:
				msg = "投资";
				layout4.setVisibility(View.VISIBLE);
				break;
			case 4:
				msg = "投资收益";
				layout4.setVisibility(View.VISIBLE);
				break;
			case 5:
				msg = "债权转让";
				layout4.setVisibility(View.VISIBLE);
				break;
			case 7:
				msg = "转让扣款";
				break;
			case 8:
				msg = "体验金收益"; /** 8.体验金收益 add by wuwenfeng 2015-10-23 **/
				layout4.setVisibility(View.VISIBLE);
				break;
			default:
				break;
		}
		return msg;
	}
	
	@Override
	public void onErrorResponse(VolleyError error) {
		super.onErrorResponse(error);
		error.printStackTrace();
	}
	
	private String getTradeResultMsg(int type, int status) {
		String msg = "";
		int flag = -1;
		switch (type) {
			case 1:
				if(status == 1) {
					msg = "充值成功";
					flag = 1;
				} else if(status == 0) {
					msg = "充值失败";
					flag = 0;
				}
				break;
			case 2:
			case 6:
				if(status == 1) {
					msg = "已审核，待打款";
					flag = 1;
				} else if(status == 0) {
					msg = "打款失败";
					trade_reason.setVisibility(View.VISIBLE);
					isRemit = true;
					flag = 0;
				} else if(status == 2) {
					msg = "提现成功";
					flag = 1;
				} else if(status == 3) {
					msg = "已提交，待审核";
					flag = 1;
				} else if(status == 4) {
					msg = "审核未通过";
					trade_reason.setVisibility(View.VISIBLE);
					isRemit = false;
					flag = 0;
					
				}
				break;
			case 4:
			case 8:/** 8.体验金收益 add by wuwenfeng 2015-10-23 **/
				if(status == 0) {
					msg = "交易成功";
					flag = 1;
				}
				break;
			case 3:
				if(status == 1) {
					msg = "投资成功";
					flag = 1;
				} else if(status == 2) {
					msg = "投资失败";
					flag = 0;
				}
				break;
			case 5:
				if(status == 1) {
					msg = "转让成功";
					flag = 1;
				}  else if(status == 2) {
					msg = "转让失败";
					flag = 0;
				}
				break;
			case 7:
				if(status == 1) {
					msg = "扣款成功";
					flag = 1;
				}
				break;

		}
		if(flag == 1) {
			icon.setImageResource(R.drawable.trade_success);
		} else if(flag == 0) {
			icon.setImageResource(R.drawable.trade_failure);
			trade_flag.setTextColor(getResources().getColor(R.color.color_ef1d0b));
		}
		return msg;
	}
	
	private void getTransactionDetail(String userId, int typeId, String projectId) {
		showLoadingDialog(getString(R.string.common_loading));
		JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
		ajaxParams.putCommonTypeParam("userId", userId);
		ajaxParams.putCommonTypeParam("typeId", typeId);
		ajaxParams.putCommonTypeParam("projectId", projectId);
		FormResultRequest<TransactionRecordBean> request = new FormResultRequest<TransactionRecordBean>(UrlConstants.GETTRANSACTIONDETAIL, this, this, new TypeToken<RequestResult<TransactionRecordBean>>() {
		}.getType());
		request.setRequestParams(ajaxParams);
		executeRequest(request);
	}

	@Override
	public void onResponse(RequestResult<TransactionRecordBean> arg0) {
		dismissLoadingDialog();
		if(arg0.flag == 1) {
			trade_flag.setText(getTradeResultMsg(arg0.getRs().getCapitalType(), arg0.getRs().status));
			name_detail.setText(getMsg(arg0.getRs().getCapitalType()));
			if(arg0.getRs().getIsAddOrCut() == 1) {
				trade_amount_detail.setText("+" + MoneyShowTool.twolastDF(Double.parseDouble(arg0.getRs().getAmount())));
			} else if(arg0.getRs().getIsAddOrCut() == 2) {
				
				trade_amount_detail.setText("-" + MoneyShowTool.twolastDF(Double.parseDouble(arg0.getRs().getAmount())));
			}
			if(trade_reason.getVisibility() == View.VISIBLE) {
				if(isRemit) {
					trade_reason.setText(arg0.getRs().remittRemark);
				} else {
					trade_reason.setText(arg0.getRs().getRemark());
				}
			}
			if(layout1.getVisibility() == View.VISIBLE) {
				bank_name_detail.setText(arg0.getRs().bankName);
				poundage_layout.setVisibility(View.VISIBLE);
				withdraw_poundage_detail.setText(MoneyShowTool.twolastDF(arg0.getRs().chargeAmount) + "元");
			}
			if(layout2.getVisibility() == View.VISIBLE) {
				bank_no_detail.setText("尾号" + arg0.getRs().bankAccount.substring(arg0.getRs().bankAccount.length() - 4));
			}
			if(layout3.getVisibility() == View.VISIBLE) {
				recharge_channel_detail.setText(getRechargeWay(arg0.getRs().rechargeWay));
			}
			if(layout4.getVisibility() == View.VISIBLE) {
				opposite_info_detail.setText("人人创");
			}
			if(TextUtils.isEmpty(arg0.getRs().qn)) {
				serial_number_detail.setText("——");
			} else {
				serial_number_detail.setText(arg0.getRs().qn);
			}
			order_no_detail.setText(arg0.getRs().orderNo);
			trade_time_detail.setText(DateTimeTool.ymdhmsDate(arg0.getRs().getCreateDt()));
		}
	}

	private String getRechargeWay(int type) {
		String msg = "";
		switch (type) {
			case 1:
				msg = "网银";
				break;
			case 2:
				msg = "支付宝";
				break;
			case 3:
				msg = "后台充值";
				break;
			case 4:
				msg = "连连支付";
				break;
		}
		return msg;
	}
}
